<template>
    <div class="update-name">
      <!-- 导航栏 -->
      <van-nav-bar
       title="昵称"
       left-text="取消"
       right-text="完成"
       @click-left="$emit('close')"
       @click-right="onConfirm"
      />
      <!-- /导航栏 -->
      <!-- /个人信息 -->
      <div class='field-wrap'>
        <van-field
          v-model.trim="localName"
          rows="2"
          autosize
          type="textarea"
          maxlength="7"
          show-word-limit
         />
      </div>
    </div>
  </template>

<script>
import { updateUserProfile } from '@/api/user'
export default {
  name: 'UpdateName',
  components: {},
  props: {
    value: {
      type: String,
      required: true

    }
  },
  data () {
    return {
      localName: this.value
    }
  },
  computed: {},
  watch: {},
  created () {},
  mounted () {},
  methods: {
    async onConfirm () {
      if (this.localName === this.value) {
        this.$toast('昵称并未改变')
        return
      }
      if (!this.localName) {
        this.$toast('昵称不能为空')
      }
      try {
        this.$toast.loading({
          duration: 0, // 持续时间，0表示持续展示不停止
          forbidClick: true, // 是否禁止背景点击
          message: '昵称修改中...' // 提示消息
        })
        await updateUserProfile({
          name: this.localName
        })
        this.$emit('input', this.localName)
        this.$emit('close')
        this.$toast.success('昵称更新成功')
      } catch (err) {
        window.console.log(err)
        this.$toast.fail('昵称更新失败')
      }
    }
  }
}
</script>

  <style scoped lang="less">
 .field-wrap {
  padding: 20px;
  background-color: #d8d8d8;
}
  </style>
